package com.eascs.admin.o2o.loan.notify.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.notify.O2OMsgNotifyService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFNotifyParam;

/**
 * 征信流程
 */
@Service
public class ZXMsgNotifyServiceImpl implements O2OMsgNotifyService {
	private static Logger log = LoggerFactory.getLogger(ZXMsgNotifyServiceImpl.class);

	@Override
	public ModelResult<O2oLoanapplyEntity> notify(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapply){
		ModelResult<O2oLoanapplyEntity> modelResult =new ModelResult<>();
		log.info("启动征信流程通知:[{}]", StatusContexts.WFTYPE_ZX);
		
		loanapply = zxFlow(notifyParam,loanapply);
		
		modelResult.withModel(loanapply);
		return modelResult;
	}

	@Override
	public List<String> getServiceName() {
		ArrayList<String> list = new ArrayList<String>();
		list.add(ProcessTemplate.PROECESS_TYPE_CREDIT);
		list.add(ProcessTemplate.PROECESS_TYPE_CREDI_FQ);
		list.add(ProcessTemplate.PROECESS_TYPE_CREDI_BP);
		list.add("PROCESS_ACCESS");
		return list;
	}

	@Override
	public Boolean checkReturn() {
		return Boolean.FALSE;
	}
	
	/**
	 * 征信流程
	 * 
	 * @param notifyParam
	 * @param loanapplyEntity
	 * @return
	 */
	private O2oLoanapplyEntity zxFlow(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapplyEntity) {
		if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_PASS) {// 流程通过
			log.info("贷款编号:[{}],征信流程完成,修改为主表状态为：[{}]", loanapplyEntity.getBillCode(), StatusContexts.O2O_ZX_END);
			loanapplyEntity.setWfType(StatusContexts.O2O_ZX_END);
			// 分期贷和接力贷银行,强制修改为EA
			if (loanapplyEntity.getLoanType().equals(StatusContexts.LOAN_TYPE_EA) || loanapplyEntity.getLoanType().equals(StatusContexts.LOAN_TYPE_FQ)) {
				loanapplyEntity.setBankType("EA");
			}
		} else if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_TURNED_DOWN) {// 流程驳回
			log.info("贷款编号:[{}],征信流程驳回,修改为主表状态为：[{}]", loanapplyEntity.getBillCode(), StatusContexts.O2O_NEW);
			loanapplyEntity.setWfType(StatusContexts.O2O_NEW);
		} else if (notifyParam.getProcessStatus().equals(ProcessTaskMain.PROECESS_STATUS_REJECT)) {// 作废
			log.info("贷款编号:[{}],征信流程作废,修改为主表状态为：[{}]", loanapplyEntity.getBillCode(), StatusContexts.O2O_FK_OVER);
			loanapplyEntity.setWfType(StatusContexts.O2O_FK_OVER);
		}
		return loanapplyEntity;
	}
	
}
